import java.sql.ClientInfoStatus;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: qian
 * Date: 2022-07-06
 * Time: 16:49
 */
public class MyBinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val){
            this.val=val;
        }
    }

    /**
     * 创建一颗二叉树，返回它的根节点
     * @return
     */
    public TreeNode createTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;
        return A;
    }
    //前序遍历
    void prevOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");
        prevOrder(root.left);
        prevOrder(root.right);
    }
    //中序遍历
    void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后序遍历
    void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    /**
     * 求节点个数遍历思路
     */
    public static int count;
    void size(TreeNode root){
        if(root==null){
            return;
        }
        count++;
        size(root.left);
        size(root.right);
    }

    /**
     * 子问题思路
     * @param root
     * @return
     */
    int size2(TreeNode root){
        if(root==null) return 0;

        return size2(root.left)+size2(root.right)+1;
    }
    /**
     * 求叶子结点个数
     * 遍历思路
     */
    public static int leafCount;
    void leafSize(TreeNode root){
        if(root==null)return;
        if(root.left==null&&root.right==null) leafCount++;
        leafSize(root.left);
        leafSize(root.right);
    }
    /**
     * 求叶子结点个数
     * 子问题思路
     */
    int leafSize2(TreeNode root){
        if(root==null) return 0;
        if(root.left==null &&root.right==null){
            return 1;
        }
        return leafSize2(root.left)+leafSize2(root.right);
    }
    // 子问题思路-求第 k 层结点个数
    int getKLevelSize(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(k==1){
            return 1;
        }
        return getKLevelSize(root.left,k-1)+getKLevelSize(root.right,k-1);
    }
    // 获取二叉树的高度
    //时间复杂度：O(n)
    int getHeight(TreeNode root){
        if(root==null)return 0;
        int leftH=getHeight(root.left);
        int rightH=getHeight(root.right);
        return leftH>rightH ? leftH+1 :rightH+1;
        /*if(root.left==null &&root.right==null){
            return 1;
        }
        return getHeight(root.left)>getHeight(root.right) ? getHeight(root.left)+1 : getHeight(root.right)+1;*/
    }
    // 查找 val 所在结点，没有找到返回 null
    // 按照 根 -> 左子树 -> 右子树的顺序进行查找
    // 一旦找到，立即返回，不需要继续在其他位置查找
    TreeNode find(TreeNode root, char val){
        if(root==null){
            return null;
        }
        if(root.val==val) return root;
        TreeNode ret=find(root.left,val);
        if(ret!=null){
            return ret;
        }
        ret=find(root.right,val);
        if(ret!=null){
            return ret;
        }
        return null;
    }
    void levelOrder(TreeNode root){
        if(root==null) return;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }

    /**
     * 非递归实现前序遍历
     */
    public List<Character> prevOrderNor(TreeNode root){
        List<Character> ret=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.empty()) {
            while (cur != null) {
                ret.add(cur.val);
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top=stack.pop();
            cur=top.right;
        }
        return ret;
    }

    /**
     * 中序遍历的非递归实现
     * @param root
     * @return
     */
    public List<Character> inOrderNor(TreeNode root){
        List<Character> ret=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        if(root==null){
            return ret;
        }
        TreeNode cur=root;
        while(cur!=null || !stack.empty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.pop();
            ret.add(top.val);
            cur=top.right;
        }
        return ret;
    }
    /**
     * 后序遍历的非递归实现
     * @param root
     * @return
     */
    public List<Character> postOrderNor(TreeNode root){
        List<Character> ret=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        if(root==null){
            return ret;
        }
        TreeNode cur=root;
        TreeNode prev=null;//标记已经打印过的节点
        while(cur!=null || !stack.empty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode top=stack.peek();
            if(top.right==null || top.right==prev){
                ret.add(top.val);
                stack.pop();
                prev=top;//重要
            }else{
                cur=top.right;
            }
        }
        return ret;
    }


}
